package public

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
)

func (c *Controller) Activities() {
	var activities []models.OrganizationActivity
	offset := c.GetString("offset")
	limit := c.GetString("limit")
	qs := db.DB.Preload("Organization").Preload("OrganizationAdmin")
	organizationID, err := c.GetInt("organization_id")
	if err == nil {
		qs = qs.Where("organization_id = ?", organizationID)
	}
	if limit == "" || limit == "0" {
		qs.Find(&activities)
	} else {
		qs.Limit(limit).Offset(offset).Find(&activities)
	}

	var total int
	db.DB.Model(&models.OrganizationActivity{}).Count(&total)

	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["total"] = total
	result["activities"] = activities
	c.Data["json"] = result
	c.ServeJSON()
}
